/**
 * ChatClient.java
 */
package com.tobeface.sns.infrastructure.amqp;

import java.io.IOException;

import com.kissme.core.helper.JsonHelper;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.GetResponse;
import com.tobeface.sns.domain.message.ChatMessage;
import com.tobeface.sns.infrastructure.amqp.config.AmqpChatClientConfiguration;

/**
 * @author Dylan
 * @time 2012-8-2
 */
public class ChatMessageCustomer {

	private AmqpChatClientConfiguration configuration;
	
	public ChatMessageCustomer() {
		this.configuration = new AmqpChatClientConfiguration();
	}

	public ChatMessage getMessage() {
		try {
			Channel channel = configuration.getChannel();
			GetResponse response = channel.basicGet(configuration.getQueueName(), true);
			if (null != response) {
				byte[] body = response.getBody();
				String msg = new String(body);
				ChatMessage message = JsonHelper.fromJsonString(msg, ChatMessage.class);
				return message;
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			configuration.destroy();
		}
		return null;
	}

}
